<template>
  <div title="职位管理">
    <div>
      <el-tag style="margin-right: 8px">职位名称</el-tag>
      <el-input
          size="small"
          maxlength="20"
          v-model="pos.name"
          placeholder="请输入内容"
          show-word-limit
          @keydown.enter.native="addPosMana"
          class="posManaInput">
      </el-input>
      <el-button
          @click="addPosMana"
          icon="el-icon-circle-plus"
          type="primary"
          size="small">添加
      </el-button>
      <div class="posManaTable">
        <el-table
            @selection-change="handleSelectionChange"
            :data="tableData"
            style="width: 100%">
          <el-table-column
              type="selection"
              width="55">
          </el-table-column>
          <el-table-column
              prop="id"
              label="编号"
              width="55">
          </el-table-column>
          <el-table-column
              prop="name"
              label="职位名称"
              width="180">
          </el-table-column>
          <el-table-column
              prop="createDate"
              label="日期"
              width="200">
          </el-table-column>
          <el-table-column
              prop="enabled"
              label="是否启用"
              width="100">
          </el-table-column>

          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button
                  size="mini"
                  type="primary"
                  @click="openEditDialog(scope.$index, scope.row)">编辑
                <i class="el-icon-edit"></i>
              </el-button>
              <el-button
                  size="mini"
                  type="danger"
                  @click="handleDelete(scope.$index, scope.row)">删除
                <i class="el-icon-delete"></i>
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-button
            size="small"
            type="danger"
            style="margin-top: 10px"
            :disabled="this.multipleSelection.length==0"
            @click="batchDel">
          批量删除
        </el-button>
      </div>
      <el-dialog
          title="编辑职位名称"
          :visible.sync="dialogVisible"
          width="30%">
        <div>
          <el-tag style="margin-right: 8px">职位名称</el-tag>
          <el-input
              size="small"
              style="width: 60%"
              v-model="updatePos.name">
          </el-input>

        </div>
        <span slot="footer" class="dialog-footer">
          <el-button size="small" @click="dialogVisible = false">取 消</el-button>
          <el-button size="small" type="primary" @click="doUpdate">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
export default {
  name: "PosMana",
  data() {
    return {
      pos: {
        name: '',
      },
      updatePos: {
        name: '',
      },
      updatePreName: '-1',
      tableData: [],
      dialogVisible: false,
      multipleSelection: []
    }
  },
  mounted() {
    this.initTableData()
  },
  methods: {
    initTableData() {
      this.getRequest('/system/basic/position/').then(resp => {
        if (resp) {
          this.tableData = resp;
        }
      })
    },
    handleDelete(index, data) {
      this.$confirm('是否永久删除[' + data.name + ']职位?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteRequest('/system/basic/position/' + data.id).then(resp => {
          if (resp) {
            this.initTableData();
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });

    },
    doUpdate() {
      if (this.updatePreName == this.updatePos.name.trim()) {
        this.dialogVisible = false;
        return;
      }
      this.updatePos.createDate = '';
      this.putRequest('/system/basic/position/', this.updatePos).then(resp => {
        if (resp) {
          this.dialogVisible = false;
          this.initTableData();
        }
      })
    },
    openEditDialog(index, data) {
      Object.assign(this.updatePos, data);
      this.dialogVisible = true;
      this.updatePreName = data.name.trim();
    },
    addPosMana() {
      if (this.pos.name.trim()) {
        this.postRequest('/system/basic/position/', this.pos).then(resp => {
          if (resp) {
            this.initTableData();
            this.pos.name = '';
          } else {
            this.$message({
              message: '添加失败',
              type: 'error'
            });
          }
        })
      } else {
        this.$message.error('职位名称不能为空');
      }

    },
    batchDel() {
      this.$confirm('是否永久删除多条职位?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let multipleStr = JSON.stringify(this.multipleSelection)
        let multipleObj = JSON.parse(multipleStr)
        let ids = []
        multipleObj.forEach(item => {
          ids.push(item.id)
        })
        if (ids) {
          this.deleteRequest('/system/basic/position/', ids).then(resp => {
            if (resp) {
              this.initTableData();
            }
          })
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });


    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
      console.log(this.multipleSelection);
    }
  }
}
</script>

<style scoped>
.posManaInput {
  width: 300px;
  margin-right: 10px;
}

.posManaTable {
  margin-top: 8px;
}

</style>